package com.cskaoyan.dao;

import com.cskaoyan.utils.MybatisUtils;
import com.cskaoyan.vo.User;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class UserDaoImpl implements UserDao {


    @Override
    public User selectById(Integer id) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        User user = sqlSession.selectOne("com.cskaoyan.dao.UserDao.selectById", 1001);
        sqlSession.close();
        return user;
    }

    @Override
    public Integer insertIntoUser(User user) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        // 执行sql语句
        int effectedRows = sqlSession.insert("com.cskaoyan.dao.UserDao.insertIntoUser", user);
        // 注意别忘了提交
        sqlSession.commit();
        sqlSession.close();
        return effectedRows;
    }

    @Override
    public Integer updateUserById(User user) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        int effectedRows = sqlSession.insert("com.cskaoyan.dao.UserDao.updateUserById", user);
        // 注意别忘了提交
        sqlSession.commit();
        return effectedRows;
    }

    @Override
    public List<User> selectAll() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        List<User> users = sqlSession.selectList("com.cskaoyan.dao.UserDao.selectAll");
        sqlSession.close();
        return users;
    }

    @Override
    public Integer deleteById(Integer id) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        int effectedRows = sqlSession.delete("com.cskaoyan.dao.UserDao.deleteById",id);
        sqlSession.commit();

        return effectedRows;
    }
}
